﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
using System.Text.RegularExpressions;
//using feedit


namespace wp7CattoFreeAppv2
{
    public partial class pgRSSReaderEx : PhoneApplicationPage
    {
        #region"Events"
        private void btnGo_Click(object sender, RoutedEventArgs e)
        {

            WebClient _client = new WebClient();
            _client.DownloadStringCompleted += Feed;
            _client.DownloadStringAsync(new Uri((txtboxLocation.Text)));


        }

        private void btnMainMenu_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/CattoMainMenu.xaml", UriKind.Relative));
        }

        #endregion

        #region"Methods"

        private void Feed(object Sender, DownloadStringCompletedEventArgs e)
        {
            XElement _xml;
            try
            {
                if (!e.Cancelled)
                {
                    _xml = XElement.Parse(e.Result);
                    Results.Items.Clear();
                    foreach (XElement value in _xml.Elements("channel").Elements("item"))
                    {
                        FeedItem _item = new FeedItem();
                        _item.Title = value.Element("title").Value;
                        _item.Description = Regex.Replace(value.Element("description").Value,
                        @"<(.|\n)*?>", String.Empty);
                        _item.Link = value.Element("link").Value;
                        _item.Guid = value.Element("guid").Value;
                        _item.Published = DateTime.Parse(value.Element("pubDate").Value);
                        Results.Items.Add(_item);
                    }
                }
            }
            catch
            {
                // Ignore Errors
                NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
            }
        }

        public pgRSSReaderEx()
        {
            InitializeComponent();
            //txtboxLocation.Text = "http://feeds.feedburner.com/Catto";
            txtboxLocation.Text = "http://sports-ak.espn.go.com/espn/rss/news";
        }
        #endregion







    }
}